package com.hengtongxing.hejiayun.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hengtongxing.hejiayun.R;
import com.hengtongxing.hejiayun.base.DataCallBack;
import com.hengtongxing.hejiayun.bean.MyHouseListBean;
import com.hengtongxing.hejiayun.homepage.adapter.MyHouseListAdapter;
import com.hengtongxing.hejiayun.mine.model.MineModelImpl;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author:HWC
 */
public class MyHouseDialog extends DialogFragment {


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private Activity mActivity;
    private Dialog dialog;
    private List<MyHouseListBean.DataBean> beanList;
    private OnCallBackListener onCallBackListener;

    public void setOnCallBackListener(OnCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }

    private MyHouseListAdapter listAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initWindow();
        View view = inflater.inflate(R.layout.dialog_my_house, null);
        ButterKnife.bind(this, view);
        mActivity = getActivity();
        initView();
        requestData();
        return view;
    }


    public static MyHouseDialog newInstance() {
        Bundle args = new Bundle();
        MyHouseDialog fragment = new MyHouseDialog();
        fragment.setArguments(args);
        return fragment;
    }

    //    初始化请求
    private void initView() {
        beanList = new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        listAdapter = new MyHouseListAdapter(beanList);
        recyclerView.setAdapter(listAdapter);

        listAdapter.setOnCallBackListener(position -> {
            if (onCallBackListener != null) {
                String address = beanList.get(position).getComm_name() + beanList.get(position).getBuilding() + beanList.get(position).getUnit_name() + beanList.get(position).getFloor() + beanList.get(position).getHose_number();
                onCallBackListener.onCallBack(address, beanList.get(position).getId() + "");
            }
            dismiss();
        });
    }

    public void requestData() {
        new MineModelImpl().requestMyHouse(new DataCallBack<MyHouseListBean>() {
            @Override
            public void onSuccessful(MyHouseListBean bean) {
                if (bean.getData().size() > 0) {
                    beanList.addAll(bean.getData());
                }
                listAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    //设置window属性
    private void initWindow() {
        //去掉dialog的标题
        dialog = this.getDialog();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);
        Window window = dialog.getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        window.getAttributes().windowAnimations = R.style.commomDialog;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    public interface OnCallBackListener {
        void onCallBack(String name, String id);
    }
}
